package com.yupi.yupicturebackend.controller;

import com.yupi.yupicturebackend.api.baidu.imageRecognition.BaiduLandmarkApi;
import com.yupi.yupicturebackend.common.BaseResponse;
import com.yupi.yupicturebackend.common.ResultUtils;
import com.yupi.yupicturebackend.manager.auth.annotation.SaSpaceCheckPermission;
import com.yupi.yupicturebackend.manager.auth.model.SpaceUserPermissionConstant;
import com.yupi.yupicturebackend.model.dto.pos.POSPictureUploadRequest;
import com.yupi.yupicturebackend.model.vo.PictureVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 @author 边拾柱
 @date 2025/4/2 13:40
 */
@RequestMapping("/pos")
@RestController
@Api(tags = "图片科普接口")
public class PopularizationOfScienceController {
    @Resource
    private BaiduLandmarkApi baiduLandmarkApi;
    /**
     * 上传图片（可重新上传）
     */
    @PostMapping("/upload")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "图片File科普")
    public BaseResponse<PictureVO> uploadPicture(
            @RequestPart("file") MultipartFile multipartFile,
            HttpServletRequest request) {
//        User loginUser = userService.getLoginUser(request);
        PictureVO pictureVO = new PictureVO();
        return ResultUtils.success(pictureVO);
    }

    /**
     * 上传图片
     */
    @PostMapping("/upload/url")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "图片Url科普")
    public BaseResponse<String> uploadPictureByUrl(
            @RequestBody POSPictureUploadRequest posPictureUploadRequest,
            HttpServletRequest request) throws Exception {
//        User loginUser = userService.getLoginUser(request);
        String locationName = baiduLandmarkApi.recognizeLandmarkByUrl(posPictureUploadRequest.getImgUrl(), "");
        return ResultUtils.success(locationName);
    }


    /**
     * 直接传递base64编码
     * @param request
     * @return
     */
    @PostMapping("/upload/base64")
    @ApiOperation(value = "图片base64科普")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<String> uploadImage(@RequestBody POSPictureUploadRequest request) throws Exception {
        // 如果接收完整Data URL
        String fullBase64 = request.getImageData();

        // 如果只接收纯Base64，可以这样重构完整Data URL
//        ...
//        String imageData = "data:" + request.getImageType() + ";base64," + request.getImageData();

        String locationName = baiduLandmarkApi.recognizeLandmarkByBase64(fullBase64, "");
        // 处理图片...
        return ResultUtils.success(locationName);
    }
}
